class SummaryRanges(object):

    def __init__(self):
        self.n = 10 ** 4 + 1
        self.tree = [0] * 4 * (self.n + 1)
        self.tree_list = [[] for _ in range(4 * (self.n + 1))]

    def add(self, node, val, left, right):
        if not self.tree[node]:
            if left == right:
                self.tree_list[node] = [[left, right]]
                self.tree[node] = True
                return True
            else:
                mid = (left + right) // 2
                if val <= mid:
                    left_flag = self.add(2 * node + 1, val, left, mid)
                    left_list = self.tree_list[2 * node + 1]
                    right_flag = self.tree[2 * node + 2]
                    right_list = self.tree_list[2 * node + 2]
                else:
                    left_flag = self.tree[2 * node + 1]
                    left_list = self.tree_list[2 * node + 1]
                    right_flag = self.add(2 * node + 2, val, mid + 1, right)
                    right_list = self.tree_list[2 * node + 2]
                if left_list and right_list and left_list[-1][1] + 1 == right_list[0][0]:
                    self.tree_list[node] = left_list[:-1] + [[left_list[-1][0], right_list[0][1]]] + right_list[
                                                                                                     1:]
                else:
                    self.tree_list[node] = left_list + right_list
            return left_flag & right_flag
        else:
            return True

    def addNum(self, val):
        """
        :type val: int
        :rtype: None
        """
        self.add(0, val, 0, self.n)

    def getIntervals(self):
        """
        :rtype: List[List[int]]
        """
        return self.tree_list[0]


obj = SummaryRanges()
obj.addNum(1)
print(obj.getIntervals())
obj.addNum(3)
print(obj.getIntervals())